package cn.spikego.advicelite.common

import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.Reader
import java.net.URL
import java.nio.charset.StandardCharsets

object URLUtils {

@JvmStatic
@Throws(Exception::class)
fun readAllFromUrl(url: String): String {
    URL(url).openStream().use { inputStream ->
            BufferedReader(InputStreamReader(inputStream, StandardCharsets.UTF_8)).use { reader ->
        return readAll(reader)
    }
    }
}

@JvmStatic
@Throws(Exception::class)
fun readAll(rd: Reader): String {
    val sb = StringBuilder()
    var cp: Int
    while (rd.read().also { cp = it } != -1) {
        sb.append(cp.toChar())
    }
    return sb.toString()
}
}